﻿Public Partial Class CreateUser
    Inherits BaseUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Rebind()
        End If
    End Sub

    Protected Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click

        If txtUsername.Text.Trim.Length = 0 Then
            Me.Prompt("Please enter a valid Username", UserControlPromptTypes.Error)
            Exit Sub
        End If

        If txtPassword.Text <> txtPassword1.Text Then
            Me.Prompt("The Password and Confirm Password must match!", UserControlPromptTypes.Error)
            Exit Sub
        End If

        'todo: check for email pattern
        If txtEmail.Text.Trim.Length = 0 Then
            Me.Prompt("Please enter a valid Email", UserControlPromptTypes.Error)
            Exit Sub
        End If

        Try
            Dim _membershipCreateStatus As MembershipCreateStatus
            Dim _membershipUser As MembershipUser
            _membershipUser = Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, txtSecQuestion.Text, txtSecAnswer.Text, chkApproved.Checked, _membershipCreateStatus)

            Select Case _membershipCreateStatus
                Case MembershipCreateStatus.Success
                    Me.Prompt("User created successfuly!", UserControlPromptTypes.Successful)
                    rptRoles.DataSource = Roles.GetAllRoles
                    rptRoles.DataBind()
                    SetRegistrationFormStatus(False)
                    Me.NotifyUpdate(sender, e)
                Case Else
                    'todo: implement message
                    Me.Prompt(_membershipCreateStatus.ToString, UserControlPromptTypes.Alert)
            End Select
        Catch ex As Exception
            Me.RaiseError(ex)
        End Try

    End Sub

    Public Overrides Sub Rebind()

        txtEmail.Text = ""
        txtPassword.Text = ""
        txtPassword1.Text = ""
        txtSecAnswer.Text = ""
        txtSecQuestion.Text = ""
        txtUsername.Text = ""
        chkApproved.Checked = False

        SetRegistrationFormStatus(True)

    End Sub

    Private Sub SetRegistrationFormStatus(ByVal activate As Boolean)

        btnCreateUser.Enabled = activate
        txtEmail.Enabled = activate
        txtPassword.Enabled = activate
        txtPassword1.Enabled = activate
        txtSecAnswer.Enabled = activate
        txtSecQuestion.Enabled = activate
        txtUsername.Enabled = activate
        chkApproved.Enabled = activate
        rptRoles.Visible = Not activate
        btnAddRoles.Visible = Not activate

    End Sub

    Protected Sub btnAddRoles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRoles.Click
        Try
            Dim roleNames As String = ""
            For Each dataRow As RepeaterItem In rptRoles.Items
                Dim chkRole As CheckBox = dataRow.FindControl("chkRole")
                If chkRole.Checked Then
                    roleNames += chkRole.Text & ";"
                End If
            Next

            'Checkig if any role selected 
            If roleNames.Length > 0 Then
                'Removing last ;
                roleNames = roleNames.Remove(roleNames.Length - 1, 1)
                Roles.AddUserToRoles(txtUsername.Text, roleNames.Split(";"))
                Me.Prompt("Roles assigned successfuly!", UserControlPromptTypes.Successful)
            Else
                Me.Prompt("Any role didn't assign to the user!", UserControlPromptTypes.Alert)
            End If

            SetRegistrationFormStatus(True)
            Me.NotifyUpdate(sender, e)

        Catch ex As Exception
            Me.RaiseError(ex)
        End Try
    End Sub

End Class
